package me.pearjelly.dao.hibernate;

import java.util.List;

import me.pearjelly.dao.PicDao;
import me.pearjelly.model.Pic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository
public class PicDaoImpl extends AbstractDao implements PicDao {

	@Override
	public Long save(Pic pic) {
		try {
			return (Long) getHibernateTemplate().save(pic);
		} catch (DataAccessException e) {
			logger.error(e.getMessage());
			throw e;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pic findByMd5(String md5) {
		try {
			List<Pic> pics = (List<Pic>) getHibernateTemplate().find(
					"from Pic p where p.md5=?", md5);
			if (pics != null && pics.size() > 0) {
				return pics.get(0);
			} else {
				return null;
			}
		} catch (DataAccessException e) {
			logger.error(e.getMessage());
			throw e;
		}
	}

	private static final Logger logger = LoggerFactory
			.getLogger(PicDaoImpl.class);
}
